package z_transformation;

import java.util.ArrayList;
import java.util.List;

class Test {
    public static void main(String[] args) {

        System.out.println(new Solution().convert("AB", 1));
    }
}

public class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        List<StringBuilder> numStr = new ArrayList<>();
        for (int i = 0; i < Math.min(numRows, s.length()); i++)
            numStr.add(new StringBuilder());
        int curRow = 0;
        boolean goDown = false;
        for (char c : s.toCharArray()) {
            numStr.get(curRow).append(c);
            if (curRow == 0 || curRow == numRows - 1) goDown = !goDown;
            curRow += goDown ? 1 : -1;
        }
        StringBuilder res = new StringBuilder();
        for (StringBuilder str :
                numStr)
            res.append(str);


        return res.toString();
    }
}
